package org.ikross.twitter.connector.factory.atom;

import java.net.MalformedURLException;
import java.net.URL;

import org.ikross.twitter.connector.AuthenticationConnector;
import org.ikross.twitter.connector.DefaultConnector;
import org.ikross.twitter.connector.IConnector;
import org.ikross.twitter.connector.factory.ConnectorFactory;
import org.ikross.twitter.exception.IllegalStateException;
import org.ikross.twitter.exception.MalformedURLConnectorException;
import org.ikross.twitter.exception.NotAllowedConnectorException;

public class AtomConnectorFactory extends ConnectorFactory {
	
	public AtomConnectorFactory(){
		this.setFormatExtension(".atom");
	}
	
	private void checkSession() throws IllegalStateException {
		if(this.getSession() == null)
			throw new IllegalStateException("Session required. It must not be null");
	}
	
	private void checkAuthenticationSession() throws IllegalStateException {
		checkSession();
		
		if(this.getSession().getUserName() == null || this.getSession().getPassword() == null)
			throw new IllegalStateException("Username and password required");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#getFormatExtension()
	 */
	@Override
	protected String getFormatExtension() {
		return this.formatExtension;
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#setFormatExtension(java.lang.String)
	 */
	@Override
	protected void setFormatExtension(String formatExtension) {
		this.formatExtension = formatExtension;
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createBlockUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createBlockUserConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Block user connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createConfirmFriendshipConnector(java.lang.String, java.lang.String)
	 */
	@Override
	public IConnector createConfirmFriendshipConnector(String idUserA, String idUserB) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Confirm friendship connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyDirectMessage(java.lang.String)
	 */
	@Override
	public IConnector createDestroyDirectMessageConnector(String idDirectMessage) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Destroy direct message connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyFavoriteConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyFavoriteConnector(String idUpdate) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Destroy favorite connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyFriendshipConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyFriendshipConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Destroy friendship connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDestroyUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createDestroyUpdateConnector(String idUpdate) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Destroy update connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createDowntimeScheduleConnector()
	 */
	@Override
	public IConnector createDowntimeScheduleConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Downtime schedule connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createEndSessionConnector()
	 */
	@Override
	public IConnector createEndSessionConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkSession();
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/account/end_session")), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createFollowUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createFollowUserConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Follow user connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createFriendsTimeLineConnector()
	 */
	@Override
	public IConnector createFriendsTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException{
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/friends_timeline"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetArchiveConnector()
	 */
	@Override
	public IConnector createGetArchiveConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get archive connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFavoritesConnector()
	 */
	@Override
	public IConnector createGetFavoritesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/favorites"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFeaturedUsersConnector()
	 */
	@Override
	public IConnector createGetFeaturedUsersConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get featured users connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFollowersConnector()
	 */
	@Override
	public IConnector createGetFollowersConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get followers connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetFriendsConnector()
	 */
	@Override
	public IConnector createGetFriendsConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get friends connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetReceivedDirectMessagesConnector()
	 */
	@Override
	public IConnector createGetReceivedDirectMessagesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/direct_messages"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetRepliesConnector()
	 */
	@Override
	public IConnector createGetRepliesConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/replies"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetSentDirectMessagesConnector()
	 */
	@Override
	public IConnector createGetSentDirectMessagesConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get sent direct messages connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createGetUpdateConnector(String idUpdate) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get update connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createGetUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createGetUserConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get user connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createNewFriendshipConnector(java.lang.String)
	 */
	@Override
	public IConnector createNewFriendshipConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("New friendship connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createPublicTimeLineConnector()
	 */
	@Override
	public IConnector createPublicTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkSession();
			
			IConnector connector = new DefaultConnector(new URL("http://twitter.com/statuses/public_timeline"+this.getFormatExtension()));
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createRateLimitStatusConnector()
	 */
	@Override
	public IConnector createRateLimitStatusConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Get rate limit status connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createSendDirectMessageConnector(java.lang.String, java.lang.String)
	 */
	@Override
	public IConnector createSendDirectMessageConnector(String recipient, String message) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Send new direct message connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createSetFavoriteConnector(java.lang.String)
	 */
	@Override
	public IConnector createSetFavoriteConnector(String idUpdate)  throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Set favorite status connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createTestConnector()
	 */
	@Override
	public IConnector createTestConnector()  throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Test connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUnblockUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createUnblockUserConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Unblock user connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUnfollowUserConnector(java.lang.String)
	 */
	@Override
	public IConnector createUnfollowUserConnector(String idUser) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Unfollow user connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateDeliveryDeviceConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateDeliveryDeviceConnector(String deliveryDevice) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Update delivery device connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateLocationGETConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateLocationGETConnector(String location) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Update location connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateLocationPOSTConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateLocationPOSTConnector(String location) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Update location connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUpdateConnector(java.lang.String)
	 */
	@Override
	public IConnector createUpdateConnector(String status) throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Update connector is not applicable to Atom format");
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createUserTimeLineConnector()
	 */
	@Override
	public IConnector createUserTimeLineConnector() throws MalformedURLConnectorException, IllegalStateException {
		try {
			this.checkAuthenticationSession();
			
			IConnector connector = new AuthenticationConnector(new DefaultConnector(new URL("http://twitter.com/statuses/user_timeline"+this.getFormatExtension())), this.getSession().getUserName(), this.getSession().getPassword());
			return connector;
		}
		catch (MalformedURLException except) {
			throw new MalformedURLConnectorException(except);
		}
	}

	/* (non-Javadoc)
	 * @see org.ikross.twitter.connector.factory.ConnectorFactory#createVerifyCredentialsConnector()
	 */
	@Override
	public IConnector createVerifyCredentialsConnector() throws NotAllowedConnectorException {
		throw new NotAllowedConnectorException("Verify credentials connector is not applicable to Atom format");
	}
}